#define _CRT_SECURE_NO_WARNINGS 1


#include <iostream>
#include <vector>
#include <string>
using namespace std;
int n;
string str1, str2;
int main()
{
    cin >> n;
    cin >> str1 >> str2;
    vector<string> strs(n);
    for (int i = 0; i < n; i++) cin >> strs[i];
    if (n == 1 || str1 == "" || str2 == "")
    {
        cout << -1 << endl;
        return 0;
    }
    int ret = 0x3f3f3f3f, left = -1, right = -1, flag = 0;
    for (int i = 0; i < n; i++)
    {
        if (strs[i] == str1 || strs[i] == str2)
        {
            right = i;
            if (left == -1) left = i;
            else if (strs[right] != strs[left])
            {
                flag = 1;
                ret = min(ret, right - left);
                left = right;
            }
            else
            {
                left = right;
            }
        }
    }
    if (flag)
        cout << ret << endl;
    else cout << -1 << endl;
    return 0;
}